package com.xiaoxx.controller.coin;

import com.xiaoxx.coin.po.res.*;
import com.xiaoxx.common.base.JwtUser;
import com.xiaoxx.common.result.R;
import com.xiaoxx.model.dto.coin.CancelOrderDto;
import com.xiaoxx.model.dto.coin.CommonOrderDto;
import com.xiaoxx.model.entity.coin.BinanceApikey;
import com.xiaoxx.model.vo.coin.AccountInfoVo;
import com.xiaoxx.service.BinanceTradingService;
import com.xiaoxx.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/trading")
@Api(value = "BinanceTradingController", tags = "币安")
public class BinanceTradingController {
    @Autowired
    private BinanceTradingService tradingService;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/getAccountInfo", method = RequestMethod.GET)
    @ApiOperation("获取账户信息 只获取usdt ")
    public R<AccountInfoVo> getAccountInfo() {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new AccountInfoVo());
        }
        return R.success(tradingService.getAccountInfo(binanceApikey.getApiKey(), binanceApikey.getSecretKey()));
    }

    @RequestMapping(value = "/getAccountTrading", method = RequestMethod.GET)
    @ApiOperation("获取账户开单信息")
    public R<AccountBillingInfoResponse> getAccountTrading() {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new AccountBillingInfoResponse());
        }
        return R.success(tradingService.getAccountTrading(binanceApikey.getApiKey(), binanceApikey.getSecretKey()));
    }

    @RequestMapping(value = "/currentAllOpenOrders", method = RequestMethod.GET)
    @ApiOperation("当前所有挂单")
    public R<List<CurrentAllOpenOrdersResponse>> currentAllOpenOrders() {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new ArrayList<>());
        }
        return R.success(tradingService.currentAllOpenOrders(binanceApikey.getApiKey(), binanceApikey.getSecretKey()));
    }

    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    @ApiOperation("取消挂单")
    public R<CancelOrderResponse> cancelOrder(@Validated @RequestBody CancelOrderDto dto) {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new CancelOrderResponse());
        }
        return R.success(tradingService.cancelOrder(binanceApikey.getApiKey(), binanceApikey.getSecretKey(), dto.getSymbol(), dto.getOrderId()));
    }

    @RequestMapping(value = "/cancelOrderBatch", method = RequestMethod.POST)
    @ApiOperation("批量取消挂单")
    public R<List<CancelOrderResponse>> cancelOrderBatch(@Validated @RequestBody CancelOrderDto dto) {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new ArrayList<>());
        }
        return R.success(tradingService.cancelBatchOrders(binanceApikey.getApiKey(), binanceApikey.getSecretKey(), dto.getSymbol(), dto.getOrderIds()));
    }

    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    @ApiOperation("下单")
    public R<CommonOrderResponse> createOrder(@Validated @RequestBody CommonOrderDto dto) {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new CommonOrderResponse());
        }
        return R.success(tradingService.createOrder(binanceApikey.getApiKey(), binanceApikey.getSecretKey(), dto));
    }

    @RequestMapping(value = "/queryOrder", method = RequestMethod.GET)
    @ApiOperation("查询订单信息")
    public R<CommonOrderResponse> queryOrder(@RequestParam String symbol, @RequestParam Long orderId) {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new CommonOrderResponse());
        }
        return R.success(tradingService.queryOrder(binanceApikey.getApiKey(), binanceApikey.getSecretKey(), symbol, orderId));
    }

    @RequestMapping(value = "/queryUserTrades", method = RequestMethod.GET)
    @ApiOperation("账户成交历史")
    public R<List<UserTradesResponse>> queryUserTrades(@RequestParam(required = false) String symbol, @RequestParam(required = false) Long orderId, @RequestParam(required = false) LocalDateTime startTime, @RequestParam(required = false) LocalDateTime endTime) {
        JwtUser jwtUser = JwtUtil.getJwtUser(request);
        BinanceApikey binanceApikey = tradingService.getBinanceApikey(jwtUser.getUserId());
        if (binanceApikey == null) {
            return R.success(new ArrayList<>());
        }
        return R.success(tradingService.queryUserTrades(binanceApikey.getApiKey(), binanceApikey.getSecretKey(), symbol, orderId, startTime, endTime));
    }
}
